<%@ page import="bean.PollUser, java.sql.*, connection.*, java.util.*" %>
<jsp:useBean id="user" class="bean.PollUser" scope="request" />
<jsp:setProperty name="user" property="*"/>

<%

Connection conn = new DbConnection().getConn();

String username = (String)session.getAttribute("username");
System.out.println(username);
String sql= "SELECT Poll.ID, Poll.Name, Poll.Description FROM Poll, ParticipantList, User_participantlist " +
	"WHERE user_participantlist.Username = '"+username+"' AND "+ 
	"Poll.ID = ParticipantList.PollID AND "+
	"ParticipantList.LName = user_participantlist.lname AND "+
	"ParticipantList.MasterUsername = user_participantlist.masterusername";

System.out.println(sql);
PreparedStatement ps = conn.prepareStatement(sql);
ArrayList<String[]> polls = new ArrayList<String[]>();
boolean hasPolls = false;
try{
	ResultSet rs = ps.executeQuery();
	while(rs.next()){
		hasPolls = true;
		String temp[] = {"", "", "", ""};
		temp[0] = rs.getString("ID");
		temp[1] = rs.getString("NAME");
		temp[2] = rs.getString("DESCRIPTION");
		polls.add(temp);
		//temp[3] is reserved for status
	}
}catch(SQLException e){
	System.out.print(e.toString());
}

//now find the open question status of all polls
for(String[] s : polls){
	String pollid = s[0];
	s[3] = "0";
	sql = "SELECT DISTINCT Question.Status FROM Question, Poll, Poll_Question WHERE " +
	"Question.QuestionID = Poll_Question.QuestionID AND "+
	"Poll_Question.PollID = Poll.ID AND "+
	"Poll.ID = "+pollid;
	System.out.println(sql);
	PreparedStatement ps2 = conn.prepareStatement(sql);
	try{
		ResultSet rs2 = ps2.executeQuery();
		while(rs2.next()){
			String status = rs2.getString("STATUS");
			System.out.println("status="+status.equals("1"));
			if(status.equals("1")){
				s[3] = "1";
				break;
			}else{
				s[3] = "0";
			}
		}
		System.out.println(s[3]);
	}catch(SQLException e){
		System.out.println(e.toString());
	}
}

%>
<div class="entrytitle">
Participate
	<div class="entry">
	Polls you can participate in:
		<%
		if(hasPolls){
			%>
			<div class="tableborder"><table class="blockTable">
			<thead>
			<tr>
				<td><strong>Name</strong></td>
				<td><strong>Open</strong></td>
				<td><strong>Description</strong></td>
				<td></td>
			</tr>
			</thead>
			<tbody>
			<%
			for(String[] curr : polls){
				boolean status = curr[3].equals("1");
				if(status){%>
					<tr class="open"><%
				}else{%>
					<tr class="closed">
				<%}%>
				<td>
				<%
				out.write(curr[1]);
				%></td><td><%
				if(status){
					%><img src="/DBP/images/open.png"/><%
				}else{
					%><img src="/DBP/images/closed.png"/><%
				}
				%></td><td><%
				out.write(curr[2]);
				%></td>
				<%if(status){ %>
					<td><a href="setupPollAnswer.jsp?pollid=<%=curr[0]%>&pollName=<%=curr[1] %>"><input type="button" style="width:50px;text-align:center" class="button" value="Start"/></a></td>
				<%}else{ %>
				<td></td>
				<%} %>
				</tr><%
			}
			%>
			</tbody>
			</table></div>
			<%
		}else{
			out.write("You have no polls assigned to you. <br/> How tragic :(");
		}
		%>
	</div>
</div>

